﻿using System;
using System.Collections.Generic;
using System.Web.Http.Dependencies;
using FlitBit.Core;

namespace FlitBit.IoC.Mvc
{
	public class DepedencyScopeResolver : IDependencyScope
	{
		IContainer _container;
		public DepedencyScopeResolver(IContainer container)
		{
			if (container == null) throw new ArgumentNullException("container");
			container = _container;
		}

		public void Dispose()
		{
			Util.Dispose(ref _container);
		}

		public object GetService(Type serviceType)
		{
			return _container.NewUntyped(LifespanTracking.Automatic, serviceType);
		}

		public IEnumerable<object> GetServices(Type serviceType)
		{
			var enumerable = typeof(IEnumerable<>).MakeGenericType(serviceType);
			return (IEnumerable<object>) this._container.NewUntyped(LifespanTracking.Automatic, enumerable);
		}
	}
}
